package discard;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufProcessor;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author Created by lianlb on 2017/11/7 17:18.
 */
public class CustomFrameDecoder extends ByteToMessageDecoder {

    private int byteMaxLength;
    private static final int BUSINESS_PACKAGE_LENGTH = 110;
    String tp = "****** 缓存区 *****";
    String mTextf = "";

    public CustomFrameDecoder(int byteMaxLength) {
        this.byteMaxLength = byteMaxLength;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println(in.toString());
        int inLength = in.readableBytes();
        if (inLength > byteMaxLength) {
            in.skipBytes(inLength);
            return;
        }
        if (inLength == 0) {
            System.out.println(tp + "没有数据");
            return;
        }
        in.forEachByte(new ByteBufProcessor(){
            @Override
            public boolean process(byte value) throws Exception {
                char cValue = (char) value;
                if (cValue == '*' || cValue == 'G'
                        || cValue == 'P' || cValue == 'S') {
                    mTextf += cValue;
                }
                return true;
            }
        });
        byte[] bytes = new byte[inLength];
        String msg = "";
        in.markReaderIndex();
        System.out.println("mtextf " + mTextf + " length " + inLength);
        if ("**GPS".equals(mTextf) && inLength < BUSINESS_PACKAGE_LENGTH) {
            in.resetReaderIndex();
            System.out.println(tp + "业务数据不完整");
            return;
        }
        in.readBytes(bytes);
        msg = new String(bytes);
        System.out.println(tp + "业务数据[" + msg + "]");
        mTextf = "";
        out.add(msg);









//        if (inLength == 0) {
//            System.out.println(tp + "没有数据");
//        } else {
//            if (inLength < HEART_PACKAGE_LENGTH) {
//                in.readBytes(bytes);
//                msg = new String(bytes);
//                System.out.println(tp + "心跳包[" + msg + "]");
//            } else if (inLength < BUSINESS_PACKAGE_LENGTH){
//                in.resetReaderIndex();
//                System.out.println(tp + "业务数据不完整");
//            } else {
//                ByteBuf bb = Unpooled.buffer(3);
//                in.readBytes(bb,3);
//                int writerIndex = in.writerIndex();
//                in.readBytes(bytes,writerIndex, 5);
//                int indexOf = -1;
//                while (true) {
//                    indexOf = in.indexOf(indexOf + 1, inLength, (byte) '*');
//                    System.out.println("头信息：" + indexOf);
//                    if (indexOf == -1) {
//                        break;
//                    }
//                }
//                in.readBytes(bytes);
//                msg = new String(bytes);
//
//                System.out.println(tp + "业务数据[" + msg + "]");
//            }
//        }
    }
}
